package cn.itcast.Rainbow_self_mention.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

import cn.itcast.Rainbow_self_mention.R;

public class For_Upload_Order extends Activity {

   static ImageView tp;
    static TextView tv;
    static Animation roll ;
    RelativeLayout up;
    static String sentresult;
    static int status=0;
    static Boolean link;
    Timer timer;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload);
        tp=findViewById(R.id.tp);
        tv=findViewById(R.id.tv);
        up=findViewById(R.id.up);
        tp.setBackgroundResource(R.drawable.load);
        roll = AnimationUtils.loadAnimation(this.getApplicationContext(), R.anim.roate_animation);
        tp.startAnimation(roll);
        addTimer();
        touch();
    }
    public void finish(){
        super.finish();
        overridePendingTransition(0, 0);
    }
    public void touch(){
        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(status==2)
                {
                    setResult(1);
                    status=0;
                    finish();
                }
                else if (status==1||status==4){
                    setResult(2);
                    status=0;
                    finish();
                }
            }
        });
    }
    public void addTimer() {        //添加计时器用于设置音乐播放器中的播放进度条
        if (timer == null) {
            timer = new Timer();     //创建计时器对象
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    Looper.prepare();
                    if(link)
                    {
                        if(status==1)
                        {
                            tp=findViewById(R.id.tp);
                            tp.clearAnimation();

                        }if(status==2)
                        {
                            tp=findViewById(R.id.tp);
                            tp.clearAnimation();

                        }if(status==3)
                        {
                        tp=findViewById(R.id.tp);
                        tp.setBackgroundResource(R.drawable.load);
                        tp.startAnimation(roll);
                        }if(status==4)
                        {
                            tp=findViewById(R.id.tp);
                            tp.clearAnimation();
                        }
                    }
                    else
                        {
                        Toast.makeText(For_Upload_Order.this,"网络连接失败",Toast.LENGTH_SHORT).show();
                        }

                    Looper.loop();
                }
            };
            //开始计时任务后的5毫秒，第一次执行task任务，以后每500毫秒执行一次
            timer.schedule(task, 5, 500);
        }
    }

    public static Handler handler = new Handler() {//创建消息处理器对象
        //在主线程中处理从子线程发送过来的消息
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            sentresult = bundle.getString("sentresult");
            //Log.i("sentresult=",sentresult);
            if (sentresult.equals("网络连接错误")) {
                link = false;
                //tv.setText("网络连接失败");
            }
            if (sentresult.equals("网络连接成功")) {
                link = true;
                //tv.setText("网络连接成功");
            }
            if(sentresult.equals("订单提交完成，准备上传图片")) {
                tv.setText("订单提交完成，准备上传图片");
                status=3;
            }
            if(sentresult.equals("全部完成提交")) {
                tv.setText("订单提交完成");
                tp.clearAnimation();
                status=2;
            }
            if(sentresult.equals("没有此用户")) {
                tv.setText("没有此收件人用户");
                tp.clearAnimation();
                status=1;
            }
            if(sentresult.equals("订单提交失败")) {
                tv.setText("订单提交失败");
                tp.clearAnimation();
                status=1;
            }
            if(sentresult.equals("图片提交失败")) {
                tv.setText("订单已提交完成,图片提交失败");
                tp.clearAnimation();
                status=4;
            }
            if(sentresult.equals("正在发送图片")) {
                tv.setText("正在发送图片");
                status=3;
            }
            if(sentresult.equals("请勿重复提交订单")) {
                tv.setText("请勿重复提交订单");
                status=1;
            }
        }
    };
}
